/***************************************************
> Copyright (C) 2024 ==QWZL== All rights reserved.
> File Name: state.c
> Author:nxq
> Mail:nxq949698473@163.com 
> Created Time: 2024年07月25日 星期四 18时04分53秒
***************************************************/

#include "WPH_server.h"
#include "state.h"
#include "sys_log.h"


//水泵开关
void wphState(int sock,char*buf)
{
    struct controls stateInfo = stateUnpack(buf);//解包

    MYSQL *mysql = NULL;
  	MYSQL *rs = NULL;
    mysql = mysql_init(NULL);
    char sql[1024] = {0};
    int ret = 0;

	rs = mysql_real_connect(mysql,"localhost","","","water",0,NULL,0);
	if(rs == NULL)
	{
		LOG_E("%s\n",mysql_error(mysql));
		mysql_close(mysql);
		exit(-1);
 	}

    int value = 0;
    LOG_D("stateInfo.id = %d,stateInfo.valve = %s\n",stateInfo.id,stateInfo.valve);
    if(stateInfo.id == 1)
    {
        if(strcmp(stateInfo.valve , "close") == 0)
        {
            strcpy(sql,"update wph1 set value = 'close' where 1 order by id desc limit 1");//修改数据表中最后一行的状态为close
            ret = mysql_real_query(mysql,sql,strlen(sql));
            if(ret != 0)
            {
                LOG_E("%s\n",mysql_error(mysql));
                mysql_close(mysql);
                exit(-1);
            }
            value = 1;
            LOG_D("value = %d\n",value);
        }
        else
        {
            value = 0;
            LOG_D("value = %d\n",value);
        }
    }
    if(stateInfo.id == 2)
    {
        if(strcmp(stateInfo.valve , "close") == 0)
        {
            strcpy(sql,"update wph2 set value = 'close' where 1 order by id desc limit 1");
            ret = mysql_real_query(mysql,sql,strlen(sql));
            if(ret != 0)
            {
                LOG_E("%s\n",mysql_error(mysql));
                mysql_close(mysql);
                exit(-1);
            }
            value = 1;
            LOG_D("value = %d\n",value);
        }
        else
        {
            value = 0;
            LOG_D("value = %d\n",value);
        }

    }
   if(stateInfo.id == 3)
    {
        if(strcmp(stateInfo.valve , "close") == 0)
        {
            strcpy(sql,"update wph3 set value = 'close' where 1 order by id desc limit 1");
            ret = mysql_real_query(mysql,sql,strlen(sql));
            if(ret != 0)
            {
                LOG_E("%s\n",mysql_error(mysql));
                mysql_close(mysql);
                exit(-1);
            }
            value = 1;
            LOG_D("value = %d\n",value);
        }
        else
        {
            value = 0;
            LOG_D("value = %d\n",value);
        }
    }

    ret = send(sock,&value,4,0);
    if(ret == -1)
    {
        LOG_E("send\n");
        exit(-1);
    
    }
}

//解包水泵状态命令
struct controls stateUnpack(char*buf)
{
    struct controls info;
    memset(&info,0,sizeof(info));
    char type[20] = {0};

    //定义cJSON指针
    cJSON *json = NULL;
    //将字符串转为json格式对象
    json = cJSON_Parse(buf);

    cJSON *node = NULL;
    //由键获取值
    node = cJSON_GetObjectItem(json,"type");
    if(node == NULL)
    {
        LOG_E("can't find type\n");
        exit(-1);
    }
    if(node->type == cJSON_String)
    {
        strcpy(type,node->valuestring);
    }
    node = cJSON_GetObjectItem(json,"id");
    if(node == NULL)
    {
        LOG_E("can't find id\n");
        exit(-1);
    }
    if(node->type == cJSON_Number)
    {
        info.id = node->valueint;
    }
    node = cJSON_GetObjectItem(json,"valve");
    if(node == NULL)
    {
        LOG_E("can't find valve\n");
        exit(-1);
    }
    if(node->type == cJSON_String)
    {
        strcpy(info.valve,node->valuestring);
    }
    
    cJSON_Delete(json);	
    return info;

}